package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.service.GoodsService;
import com.ly.plus.dto.SpuDTO;
import com.ly.plus.entity.Sku;
import com.ly.plus.entity.Spu;
import com.ly.plus.entity.SpuDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ycs
 * Date: 2020/8/20
 * Time: 20:10
 * Description: No Description
 */
@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


    /**
     * 分页查询商品
     * @param key 搜索关键字
     * @param saleable //是否根据上下架查询
     * @param page //当前页
     * @param rows //一页展示总数
     *    //入口:http://api.leyou.com/api/item/spu/page?key=&saleable=true&page=1&rows=5
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>>findGoodsByPage(@RequestParam(value = "key",required = false)String key,
                                                             @RequestParam(value = "saleable",required = false)Boolean saleable,
                                                             @RequestParam(value = "page",required = false)Long page,
                                                             @RequestParam(value = "rows",required = false)Long rows){
        PageResult pageResult=   goodsService.findGoodsByPage(key,saleable,page,rows);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加spu
     * @param spuDTO//封装对象
     *      入口:http://api.leyou.com/api/item/goods
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void>saveSpu(@RequestBody SpuDTO spuDTO){
        goodsService.saveSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id修改上下架信息
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void>updateSaleable(@RequestParam("id")Long id,
                                              @RequestParam("saleable")Boolean saleable  ){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid获取sku集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<Sku>>findSkuBySpuId(@RequestParam("id")Long id){
        List<Sku> skuList= goodsService.findSkuBySpuId(id);
        return ResponseEntity.ok(skuList);
    }

    /**
     * 根据spuid获取SpuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetail>findSpuDetailById(@RequestParam("id")Long id){
       SpuDetail spuDetail= goodsService.findSpuDetailById(id);
       return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据id获取spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuById(@PathVariable("id")Long id){
        SpuDTO spuDTO=goodsService.findSpuById(id);
        return ResponseEntity.ok(spuDTO);
    }
}
